package cn.jy.careuplusdoctor.main;


import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import cn.jy.careuplusdoctor.R;
import cn.jy.careuplusdoctor.base.BaseActivity;
import cn.jy.careuplusdoctor.entity.Doctor;
import cn.jy.careuplusdoctor.http.HttpHelper;
import cn.jy.careuplusdoctor.http.HttpHelper.onHttpListener;
import cn.jy.careuplusdoctor.http.HttpUrls;
import cn.jy.careuplusdoctor.injector.IdInject;
import cn.jy.careuplusdoctor.response.LoginResponse;

public class LoginActivity extends BaseActivity {
	
	@IdInject(id = R.id.login_user_edit)
	private EditText mUser;
	@IdInject(id = R.id.login_passwd_edit)
	private EditText mPassword;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_login);
		onInJectView();
		
		//暂时屏蔽自动登录
		//loginSilent();
	}
	
	@Override
	protected void onAfterInJector() {
		// TODO Auto-generated method stub
		super.onAfterInJector();
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.btn_login:
					String user = mUser.getText().toString();
					String password = mPassword.getText().toString();
					if (TextUtils.isEmpty(user) || TextUtils.isEmpty(password)) {
						longToast(R.string.login_empty);
						return;
					}
					login(user,password);
					break;
				case R.id.btn_register:
					openActivity(RegisterActivity.class);
					break;
				case R.id.login_forget_password:
					openActivity(ForgetPasswordActivity.class);
					break;
				}
			}
		};
		findViewById(R.id.btn_login).setOnClickListener(listener);
		findViewById(R.id.btn_register).setOnClickListener(listener);
		findViewById(R.id.login_forget_password).setOnClickListener(listener);
	}
	
	/**
	 * 自动登陆
	 */
	private void loginSilent()
	{
		SharedPreferences shared = getSharedPreferences("careu_doctor",  //缓存
				Activity.MODE_PRIVATE);
		String username = shared.getString("loginName", "");
		String password = shared.getString("loginPassword", "");
		
		if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
			login(username, password);
		}
	}
	
	private void login(final String username,final String password)
	{
		showLoading(R.string.login_do);
		HttpHelper.getInstance().login(username, password, new onHttpListener<LoginResponse>() {
			
			@Override
			public void onResponse(LoginResponse data, int code) {
				hidenLoading();
				if (data !=null && code == HttpUrls.REQUEST_SUCCESS) {
					Doctor doctor = data.getDoctor();
					if (TextUtils.equals("1", data.getResType()) && doctor !=null) {
						doctor.setLoginName(username);
						doctor.setLoginPassword(password);
						saveUserToLocal(doctor);
					}
					else {
						shortToast(data.getMsg());
					}
				}
				else {
					shortToast(R.string.login_fail_reason1);
				}
			}
		});
		
	}
	
	private void saveUserToLocal(Doctor doctor)
	{
		SharedPreferences shared = getSharedPreferences("careu_doctor",  //缓存
				Activity.MODE_PRIVATE);
		Editor edit = shared.edit();
		edit.putString("loginName", doctor.getLoginName());
		edit.putString("loginPassword", doctor.getLoginPassword());
		edit.putString("doctorCode", doctor.getDoctorCode());
		edit.putString("doctorId", doctor.getDoctorId());
		edit.putString("name", doctor.getName());
		edit.putString("sex", doctor.getSex());
		edit.putString("hid", doctor.getHid());
		edit.commit();
		toMainActivity();
	}
	
	private void toMainActivity()
	{
		openActivity(MainActivity.class);
		defaultFinish();
	}
}
